package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.constants.KDAddressAreaEnum;
import cj.web.admin.constants.KDAddressStreetEnum;
import cj.web.admin.constants.KDBookingStatusEnum;
import cj.web.admin.constants.KDCardEnum;
import cj.web.admin.constants.MemuEnum;
import cj.web.admin.dao.KDCardDAO;
import cj.web.admin.domain.KDBookingPaper;
import cj.web.admin.domain.KDCard;
import cj.web.login.controllers.BaseController;

@Path("/kdcard")
public class KDCardController extends BaseController{

	private static Logger logger = Logger.getLogger(KDCardController.class.getName());
	
	@Autowired
	private KDCardDAO kdCardDao;
	
	@Get("show")
	public String show(Model model,KDCard kdCard) {
		//MemuEnum
		model.add("MemuEnum",MemuEnum.kdgsdbksj);
        EnumSet<KDCardEnum> kdCardEnum = EnumSet.allOf(KDCardEnum.class);
        model.add("kdCardEnum",kdCardEnum);
        return "portal/KDCardList.vm";
	}

	@Post("getList")
	public String queryList(KDCard kdCard) {
	
		JSONObject jo = new JSONObject();
		
		List<KDCard> objs = kdCardDao.query(kdCard);
		

		for(KDCard obj:objs)
		{
//			obj.setBeginAt(DateUtils.getMinusTime(obj.getBeginAt(), DateUtils.DATE_SMALL_STR, 0));
//			obj.setEndAt(DateUtils.getMinusTime(obj.getEndAt(), DateUtils.DATE_SMALL_STR, 0));
			obj.setInputBeginAt(DateUtils.getMinusTime(obj.getInputBeginAt(), DateUtils.DATE_SMALL_STR, 0));
			obj.setInputEndAt(DateUtils.getMinusTime(obj.getInputEndAt(), DateUtils.DATE_SMALL_STR, 0));
		}
		
		int size = kdCardDao.querySize(kdCard);
		
		jo.put("data", objs);
		jo.put("draw", kdCard.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.kdgsdbksj);
        
        KDCard obj = kdCardDao.queryById(id);
		model.add("obj", obj);
		
		return "portal/KDCardEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,KDCard kdCard,HttpServletRequest request)
	{
		kdCard.setUpdateUser(returnSessionObj(request).getAccount());
		
		JSONObject jo = new JSONObject();
		jo.put("returnFlag",Constants.returnObj.returnFlag_true);
		
		try{
			kdCardDao.update(kdCard);
		}
		catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		kdCardDao.deleteById(id);
		return "r:show";
	}
}
